package ge.telasi.tasks.ui.task;

import ge.telasi.tasks.model.Task;
import ge.telasi.tasks.ui.comp.Hyperlink;
import ge.telasi.tasks.ui.Platform;
import ge.telasi.tasks.ui.comp.ResizableLabel;
import ge.telasi.tasks.ui.UIUtils;
import ge.telasi.tasks.ui.log.LoggerUtils;
import ge.telasi.tasks.ui.user.UserLabel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import static ge.telasi.tasks.ui.UIUtils.makeBold;
import static ge.telasi.tasks.ui.UIUtils.makePlain;

/**
 * @author dimitri
 */
public class TaskHeaderPanel extends JPanel {
    private static final long serialVersionUID = 6543328175131353898L;
    Task task;
    ResizableLabel lblTitle ;
    JLabel lblNumber;
    JLabel lblPriority;
    JLabel lblDueDate;
    UserLabel lblOwner;
    JLabel lblStatus;

    public TaskHeaderPanel() {
        initComponents();
        setOpaque(true);
        setBackground(new Color(200, 200, 200));
    }

    private void initComponents() {
        GridBagConstraints c1 = new GridBagConstraints();
        GridBagConstraints c2 = new GridBagConstraints();
        GridBagConstraints c3 = new GridBagConstraints();
        GridBagConstraints c4 = new GridBagConstraints();
        c1.gridx = 0;
        c2.gridx = 1;
        c3.gridx = 2;
        c4.gridx = 3;
        c1.insets = new Insets(2, 2, 2, 2);
        c2.insets = new Insets(2, 2, 2, 2);
        c3.insets = new Insets(2, 2, 2, 2);
        c4.insets = new Insets(2, 2, 2, 2);
        c1.anchor = GridBagConstraints.LINE_END;
        c3.anchor = GridBagConstraints.LINE_END;
        c2.weightx = 0.5;
        c2.fill = GridBagConstraints.HORIZONTAL;
        c4.weightx = 0.5;
        c4.fill = GridBagConstraints.HORIZONTAL;
        setLayout(new GridBagLayout());
        JLabel jLabel1 = new JLabel("სათაური:");
        JLabel jLabel2 = new JLabel("ნომერი:");
        JLabel jLabel3 = new JLabel("პრიორიტეტი:");
        JLabel jLabel4 = new JLabel("თარიღამდე:");
        JLabel jLabel5 = new JLabel("მფლობელი:");
        JLabel jLabel6 = new JLabel("სტატუსი:");
        jLabel1.setFont(makeBold(jLabel1.getFont()));
        jLabel2.setFont(makeBold(jLabel2.getFont()));
        jLabel3.setFont(makeBold(jLabel3.getFont()));
        jLabel4.setFont(makeBold(jLabel4.getFont()));
        jLabel5.setFont(makeBold(jLabel4.getFont()));
        jLabel6.setFont(makeBold(jLabel4.getFont()));
        lblTitle = new ResizableLabel();
        lblPriority = new JLabel();
        lblNumber = new JLabel();
        lblDueDate = new JLabel();
        lblOwner = new UserLabel();
        lblStatus = new JLabel();
        lblTitle.setFont(makePlain(lblTitle.getFont()));
        lblPriority.setFont(makePlain(lblPriority.getFont()));
        lblNumber.setFont(makePlain(lblNumber.getFont()));
        lblDueDate.setFont(makePlain(lblDueDate.getFont()));
        lblStatus.setFont(makePlain(lblDueDate.getFont()));

        // Row1

        // title
        add(jLabel1, c1);

        JPanel pnTitle = new JPanel(new BorderLayout(5, 5));
        pnTitle.setOpaque(false);
        Hyperlink lnkChangeTitle = new Hyperlink("(შეცვლა)") {
            @Override
            public void onClick() {
                onSubjectChange();
            }
        };
        pnTitle.add(lblTitle, BorderLayout.CENTER);
        pnTitle.add(lnkChangeTitle, BorderLayout.EAST);
        add(pnTitle, c2);

        // number
        add(jLabel2, c3);
        add(lblNumber, c4);

        // Row 2

        add(jLabel5, c1);
        add(lblOwner, c2);

        add(jLabel6, c3);
        add(lblStatus, c4);

        // Row 3

        // priority
        add(jLabel3, c1);

        JPanel pnPriority = new JPanel(new BorderLayout(5, 5));
        pnPriority.setOpaque(false);
        Hyperlink lnkChangePriority = new Hyperlink("(შეცვლა)") {
            @Override
            public void onClick() {
                onPriorityChange();
            }
        };
        pnPriority.add(lblPriority, BorderLayout.CENTER);
        pnPriority.add(lnkChangePriority, BorderLayout.EAST);
        add(pnPriority, c2);

        // due-date
        add(jLabel4, c3);

        JPanel pnDueDate = new JPanel(new BorderLayout(5, 5));
        pnDueDate.setOpaque(false);
        Hyperlink lnkChangeDueDate = new Hyperlink("(შეცვლა)") {
            @Override
            public void onClick() {
                onPriorityChange();
            }
        };
        pnDueDate.add(lblDueDate, BorderLayout.CENTER);
        pnDueDate.add(lnkChangeDueDate, BorderLayout.EAST);
        add(pnDueDate, c4);
    }

    public Task getTask() {
        return task;
    }

    public void setTask(Task task) {
        this.task = task;
        resetSubject();
        resetPriority();
        String numb  = "№" + task.getNumber();
        if (task.getCancelariaNumber() != null && !task.getCancelariaNumber().trim().isEmpty())
            numb += " (" + task.getCancelariaNumber() + ")";
        lblNumber.setText(numb);
        lblOwner.setUser(task.getOwner());
        lblStatus.setText(TaskDecontationUtils.getStatusName(task.getTaskStatus()));
        lblStatus.setIcon(TaskDecontationUtils.getStatusIcon(task.getTaskStatus()));
    }

    private void resetSubject() {
        lblTitle.setFullText(task.getSubject());
    }

    private void resetPriority() {
        lblPriority.setText(TaskDecontationUtils.getPriorityName(task.getPriority()));
        if (task.getDueDate() == null) {
            lblDueDate.setText("(განუსაზღვრელი)");
        } else {
            DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
            lblDueDate.setText(df.format(task.getDueDate().getTime()));
        }
    }

    protected void fireTaskChange() {
        firePropertyChange("task", "dummy old value", task);
    }

    private void onSubjectChange() {
        String newSubject = JOptionPane.showInputDialog(
                UIUtils.findShell(this),
                "დავალების სათაური:",
                task.getSubject());
        boolean emptySubject = newSubject == null || newSubject.trim().length() == 0;
        if (!emptySubject && !newSubject.equals(task.getSubject())) {
            Platform pl = Platform.getDefault();
            try {
                task = pl.getFacade().changeTaskSubject(pl.getCredentials(), task, newSubject);
                resetSubject();
                fireTaskChange();
            } catch(Exception ex) {
                LoggerUtils.manageException(this, ex);
            }
        }
    }

    private void onPriorityChange() {
        TaskPriorityChangeDialog dialog = new TaskPriorityChangeDialog(this);
        dialog.setTask(task);
        UIUtils.openDialog(dialog);
        if (dialog.isApproved()) {
            resetPriority();
            fireTaskChange();
        }
    }

}
